package edu.ptit.jad.sqlserver;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import org.jsoup.Jsoup;

public class SqlDb {

	public static void main(String[] args) throws Exception {

		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

		Connection m_Connection = DriverManager.getConnection(
				"jdbc:sqlserver://123.30.186.186:1433;DatabaseName=KYO",
				"phuongdo", "diemnhan@admin123");

		Statement m_Statement = m_Connection.createStatement();
		String query = "SELECT TOP(40000) InformationID,CategoryId,TItle,Abstract,Content FROM Information ";

		ResultSet m_ResultSet = m_Statement.executeQuery(query);

		File file = new File("data/trains/news.jad");
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file, true)));

		// logger.info(banner.getBannerId() + banner.getTitle()

		int top = 40000;
		int current = 0;

		System.out.println("Estimate " + top + " records");
		System.out.println("");
		for (int i = 0; i < 100; i++) {
			System.out.print("=");
		}
		System.out.println("");
		double last_percent = 0.0;
		while (m_ResultSet.next()) {

		

			out.write(m_ResultSet.getString("InformationID") + "::::"
					+ m_ResultSet.getString("CategoryId") + "::::"
					+ Jsoup.parse(m_ResultSet.getString("Title")).text().trim().replaceAll("(\r\n|\r|\n|\n\r|\t|\t\r\n)", "") + "::::"
					+ Jsoup.parse(m_ResultSet.getString("Abstract")).text().trim().replaceAll("(\r\n|\r|\n|\n\r|\t|\t\r\n)", "") + "::::"
					+ Jsoup.parse(m_ResultSet.getString("Content")).text().trim().replaceAll("(\r\n|\r|\n|\n\r|\t|\t\r\n)", ""));
		
//			
//			out.write(m_ResultSet.getString("InformationID") + "::::"
//					+ m_ResultSet.getString("CategoryId") + "::::"
//					+ m_ResultSet.getString("Title") + "::::"
//					+ m_ResultSet.getString("Abstract") + "::::"
//					+ Jsoup.parse(m_ResultSet.getString("Content")).text().replace("\n",""));
			out.write("\n");

			current++;

			double percent = (double) current * 100 / top;

			if (percent - Math.round(percent) == 0) {

				System.err.print("=");

			}

		}

		System.out.println("");

		System.out.println("Done!");
		out.close();

	}
}
